package com.cjh.mybatis04.bind;


import com.cjh.mybatis04.session.SqlSession;
import lombok.Data;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 映射器代理工厂
 * @author: cjh
 * @date: 2023/5/23 - 20:14
 */
@Data
public class MapperProxyFactory<T> {

    private final Class<T> mapperInterface;
    private Map<Method,MapperMethod> methodCache = new ConcurrentHashMap<>();

    @SuppressWarnings("unchecked")
    public T newInstance(SqlSession sqlSession){
        MapperProxy<T> proxy = new MapperProxy<>(sqlSession, mapperInterface,methodCache);
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(),new Class[]{mapperInterface},proxy);

    }
}
